package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.ZhGameInfo;
import com.ruoyi.system.service.IZhGameInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhGood;
import com.ruoyi.system.service.IZhGoodService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.service.IZhDeptInfoService;
import com.ruoyi.common.domain.ZhDeptInfo;

/**
 * 商品 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-16
 */
@Controller
@RequestMapping("/system/zhGood")
public class ZhGoodController extends BaseController
{
    private String prefix = "system/zhGood";

	@Autowired
	private IZhGoodService zhGoodService;

	@Autowired
	private IZhDeptInfoService zhDeptInfoService;

	@Autowired
	private IZhGameInfoService zhGameInfoService;

	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:view")
	@GetMapping()
	public String zhGood(ZhGood zhGood,ModelMap mmap)
	{
		mmap.put("zhDepts" , zhDeptInfoService.selectZhDeptInfoList(buildParamsContainer(ZhDeptInfo.class,zhGood.getParams())));
        mmap.put("zhGameInfos" , zhGameInfoService.selectDeptGameList(buildParamsContainer(ZhGameInfo.class,zhGood.getParams())));

        return prefix + "/zhGood";
	}
	
	/**
	 * 查询商品列表
	 */
	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ZhGood zhGood)
	{
		startPage();
        List<ZhGood> list = zhGoodService.selectZhGoodList(zhGood);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商品列表
	 */
	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhGood zhGood)
    {
    	List<ZhGood> list = zhGoodService.selectZhGoodList(zhGood);
        ExcelUtil<ZhGood> util = new ExcelUtil<ZhGood>(ZhGood.class);
        return util.exportExcel(list, "zhGood");
    }
	
	/**
	 * 新增商品
	 */
	@RequiresPermissions("system:zhGood:add")
	@DataScope(tableAlias = "l")
	@GetMapping("/add")
	public String add(ZhGood zhGood,ModelMap mmap)
	{
		mmap.put("zhDepts" , zhDeptInfoService.selectZhDeptInfoList(buildParamsContainer(ZhDeptInfo.class,zhGood.getParams())));
		mmap.put("zhGameInfos" , zhGameInfoService.selectDeptGameList(buildParamsContainer(ZhGameInfo.class,zhGood.getParams())));

		return prefix + "/add";
	}
	
	/**
	 * 新增保存商品
	 */
	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:add")
	@Log(title = "商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ZhGood zhGood)
	{
		// 这里设置卖家用户名
		String loginName = ShiroUtils.getLoginName();
		zhGood.setSalerUsername(loginName);
		return toAjax(zhGoodService.insertZhGood(zhGood));
	}

	/**
	 * 修改商品
	 */
	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:edit")
	@GetMapping("/edit/{id}")
	public String edit(ZhGood zhGood,@PathVariable("id") Integer id, ModelMap mmap)
	{
		ZhGood result = zhGoodService.selectZhGoodById(id);
		mmap.put("zhGood", result);
		mmap.put("zhDepts" , zhDeptInfoService.selectZhDeptInfoList(buildParamsContainer(ZhDeptInfo.class,zhGood.getParams())));
		mmap.put("zhGameInfos" , zhGameInfoService.selectDeptGameList(buildParamsContainer(ZhGameInfo.class,zhGood.getParams())));

		return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品
	 */
	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:edit")
	@Log(title = "商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ZhGood zhGood)
	{		
		return toAjax(zhGoodService.updateZhGood(zhGood));
	}
	
	/**
	 * 删除商品
	 */
	@DataScope(tableAlias = "l")
	@RequiresPermissions("system:zhGood:remove")
	@Log(title = "商品", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(ZhGood zhGood,String ids)
	{		
		return toAjax(zhGoodService.deleteZhGoodByIds(ids));
	}
	
}

